package base.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class GetMethodReflect {
    public static String className = "base.demo.Person";

    public static void main(String[] args) throws Exception {
//        getMethodNames();
//        callMethodWithParams();
        methodInvoke();
    }


    public static void getMethodNames() throws ClassNotFoundException {
        Class clazz = Class.forName(className);
        Method[] publicMethods = clazz.getMethods();//获取的都是公有的方法
        Method[] methods = clazz.getDeclaredMethods();////只获取本类中所有方法，包括私有。


        for (Method method : publicMethods) System.out.println(method);

//        for (Method method:methods) System.out.println(method);

    }


    public static void callMethodWithParams() throws Exception {
        Class clazz = Class.forName(className);
        Method method = clazz.getMethod("show", null);
        Object obj = clazz.newInstance();

        Constructor constructor = clazz.getConstructor(int.class, String.class);
        obj = constructor.newInstance(45, "Jim");

        method.invoke(obj, null);
    }


    public static void methodInvoke() throws Exception {
        Class clazz = Class.forName(className);

        Method method = clazz.getMethod("paramMethod", String.class, int.class);
        Object obj = clazz.newInstance();

        Constructor constructor = clazz.getConstructor();
        obj = constructor.newInstance();

        method.invoke(obj, "Mark", 98);
    }


    public static void getClassObject6() {


    }


}




































